package com.taiji.dicom4cloud.smart.handler;

import com.netsdk.common.SavePath;
import com.netsdk.lib.NetSDKLib;
import com.sun.jna.Pointer;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;

/**
 * 抓图回调
 */
@Component
public class CaptureReceiveCB implements NetSDKLib.fSnapRev {

    BufferedImage bufferedImage = null;

    @Override
    public void invoke(NetSDKLib.LLong lLoginID, Pointer pBuf, int RevLen, int EncodeType, int CmdSerial, Pointer dwUser) {
        if(pBuf != null && RevLen > 0) {
            String strFileName = SavePath.getSavePath().getSaveCapturePath();

            System.out.println("strFileName = " + strFileName);

            byte[] buf = pBuf.getByteArray(0, RevLen);
            ByteArrayInputStream byteArrInput = new ByteArrayInputStream(buf);
            try {
                bufferedImage = ImageIO.read(byteArrInput);
                if(bufferedImage == null) {
                    return;
                }
                ImageIO.write(bufferedImage, "jpg", new File(strFileName));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
